<style lang="less" scoped>
@import './main.less';
</style>
<template>
  <div class="main" :class="{'main-hide-text': shrink}">
    <div class="sidebar-menu-con" :style="{width: shrink ?'60px':'260px'}">
      <div slot="top" :style="{background:theme.background , color:theme.color }" class="logo-con">
        <!-- <img src="../assets/images/logo.png"/> -->
        传艾之家
      </div>
      <sidebar-menu
        :shrink="shrink"
        :menu-list="menuList"
        :theme="theme"
      >
      </sidebar-menu>
    </div>
    <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'260px'}">
      <div class="main-header">
        <div class="navicon-con">
          <Button
            :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}"
            class="main-tab-btn"
            type="text"
            @click="toggleClick"
          >
            <Icon type="md-menu" style="color: #2d8cf0" size="32"/>
          </Button>
        </div>
        <div class="navicon-title">

        </div>
        <div class="header-avator-con">
          <div class="user-dropdown-menu-con">
            <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
              <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                <a href="javascript:void(0)" style="display:flex;align-items:center">
                  <span class="main-user-name">{{ userName }}</span>
                  <Icon type="ios-arrow-down" />
                </a>
                <DropdownMenu slot="list">
                  <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                </DropdownMenu>
              </Dropdown>
              <Avatar :src="avatorPath" style="background: #619fe7;margin-left: 10px;"></Avatar>
            </Row>
          </div>
        </div>
      </div>
      <div class="tags-con">
        <tags-page-opened :pageTagsList="pageTagsList"></tags-page-opened>
      </div>
    </div>
    <div class="single-page-con" :style="{top:'100px',left:shrink?'60px':'280px'}">
      <Card dis-hover>
          <div class="pageTitle" slot="title">{{$route.meta.title}}</div>
          <div class="single-page">
            <router-view></router-view>
          </div>
      </Card>
    </div>
  </div>
</template>
<script>
import less from '@/assets/js/less'
import tagsPageOpened from '@/components/tagsPage/tags-page-opened.vue'
import sidebarMenu from '@/components/shrinkable-menu/shrinkable-menu.vue'

export default {
  components: {
    sidebarMenu,
    tagsPageOpened
  },
  data() {
    return {
      screenWidth:document.body.clientWidth,
      theme:{
        background:'#191a23',
        color:'#fff'
      },
      shrink: document.body.clientWidth < 768 ? true:false,
      userName: '',
      isFullScreen: false,
    }
  },
  computed: {
    menuList() {
      return this.$store.state.app.menuList
    },
    pageTagsList() {
      return this.$store.state.app.pageOpenedList // 打开的页面的页面对象
    },
    avatorPath() {
      return window.location.protocol + '//' +  window.location.host + '/logo.png'
    },
    currentPath() {
      return this.$store.state.app.currentPath // 当前面包屑数组
    },
  },
  methods: {
    init() {
      // 监听窗口大小
      const that = this
      window.addEventListener('resize', function(){ 
          window.screenWidth = document.body.clientWidth
          that.screenWidth =  window.screenWidth
          that.shrink = this.screenWidth < 768 ? true:false
      })
      that.$store.commit('setOpenedList')
      that.checkTag(that.$route.name)
      that.userName = that.Cookies.get('user')
    },
    toggleClick() {
      this.shrink = !this.shrink
    },
    checkTag(name) {
      let openpageHasTag = this.pageTagsList.some(item => {
        if (item.name === name) {
          return true
        }
      })
      if (!openpageHasTag) {
        //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
        this.util.openNewPage(
          this,
          name,
          this.$route.params || {},
          this.$route.query || {}
        )
      }
    },
    loginout(){
      // 退出登录
      this.$store.commit('logout', this)
      this.$store.commit('clearOpenedSubmenu')
      this.$router.push({
        name: 'login'
      })
    },
    handleClickUserDropdown(name) {
      if (name === 'ownSpace') {
        this.util.openNewPage(this, 'ownspace_index')
        this.$router.push({
          name: 'ownspace_index'
        })
      } else if (name === 'loginout') {
        // 退出登录
        this.$store.commit('logout', this)
        this.$store.commit('clearOpenedSubmenu')
        this.$router.push({
          name: 'login'
        })
      }
    },
  },
  watch: {
    $route(to) {
      this.checkTag(to.name)
      localStorage.currentPageName = to.name
    },
    screenWidth (val) {
        this.screenWidth = val
        this.shrink = this.screenWidth < 768 ? true:false
    }
  },
  mounted() {
    this.init()  
  },
  created() {

  },
  dispatch() {
    
  }
}
</script>
